#include <cstdio>
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <cstring>
#include <cassert>
#include <cstdlib>
#include <queue>
#include <ctime>

#define N 32

using namespace std; 

int n; 
int a[N][N]; 
int b[N][N]; 
int color[N*N]; 


struct coor_t {
	int i, j; 
}; 

void dump_b(void) 
{
	int i, j; 
	static int cnt = 0; 
	cout<<endl<<"b[i][j]"<<endl; 
	for (i=1; i<=n; i++) {
		for (j=1; j<=n; j++) {
			cout<<b[i][j]<<" "; 
		}
		cout<<endl;
	}
	if (cnt++>1000)
		exit(0); 
}

#define DBG 0

int main()
{
	int i, j, d, e, cnt=2; 
	queue<coor_t> q; 
	coor_t dir[] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; 

	//freopen("p1162.in", "r", stdin); 
	
	for (i=0; i<N; i++) {
		for (j=0; j<N; j++) {
			a[i][j] = -1; 
		}
	}
	
	cin>>n; 
	for (i=1; i<=n; i++) {
		for (j=1; j<=n; j++) {
			cin>>a[i][j]; 
		}
	}

#if DBG
	for (i=1; i<=n; i++) {
		for (j=1; j<=n; j++) {
			cout<<a[i][j]<<" "; 
		}
		cout<<endl;
	}
#endif

	for (i=1; i<=n; i++) {
		for (j=1; j<=n; j++) {
			if (a[i][j]==1) {
				color[1] = 1; 
				b[i][j] = 1; 
				continue; 
			}
			if (b[i][j]>=1) {
				continue; 
			}
			coor_t t, c; 
			t.i = i;
			t.j = j; 
			cnt++; 
			color[cnt] = 2; 
			q.push(t); 
			while(!q.empty()) {
				t = q.front(); 
				q.pop(); 
				b[t.i][t.j] = cnt; 
				for (d=0; d<4; d++) {
					c.i = t.i+dir[d].i; 
					c.j = t.j+dir[d].j; 
					if (b[c.i][c.j]>=3) 
						continue; 
					if (a[c.i][c.j]==-1) {
						e = 0; 
					} else if (a[c.i][c.j]==0) {
						if (b[c.i][c.j]<2) {
							b[c.i][c.j] = 2; 
							q.push(c); 
						}
						e = 2; 
					} else {
						e = 2; 
						b[c.i][c.j] = 1; 
					}
					//cout<<"cnt="<<cnt<<" color[cnt]="<<color[cnt]<<" i="<<t.i<<" j="<<t.j<<" e="<<e<<endl; 
					//dump_b(); 
					color[cnt] = min(color[cnt], e); 
				}
			}
		}
	}

#if DBG
	cout<<endl<<"b[i][j]"<<endl; 
	for (i=1; i<=n; i++) {
		for (j=1; j<=n; j++) {
			cout<<b[i][j]<<" "; 
		}
		cout<<endl;
	}

	cout<<endl<<"color[b[i][j]]"<<endl; 
	for (i=1; i<=n; i++) {
		for (j=1; j<=n; j++) {
			cout<< color[b[i][j]] <<" "; 
		}
		cout<<endl;
	}
	cout<<endl<<endl; 
#endif //DBG

	for (i=1; i<=n; i++) {
		for (j=1; j<=n; j++) {
			int k; 
			k = color[b[i][j]]; 
			cout<<k<<" "; 
		}
		cout<<endl;
	}

	return 0; 
}

